package com.cuixk.microservice.consumer.controller;

import com.cuixk.microservice.consumer.entity.User;
import com.cuixk.microservice.consumer.feign.UserFeign;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/get")
public class OrderController {

    @Resource
    private LoadBalancerClient loadBalancerClient;

    @Resource
    private UserFeign userFeign;

//    @GetMapping("/{id}")
//    public String getUserInfo(@PathVariable Integer id) {
//        ServiceInstance instance = loadBalancerClient.choose("service-provider");
//        String url = instance.getUri().toString() + "/user/" + id;
//        RestTemplate restTemplate = new RestTemplate();
//        //调用provider服务
//        User result = restTemplate.getForObject(url, User.class);
//        return "调用" + instance.getServiceId() + "服务，端口号：" + instance.getPort() + "\n" + result.toString();
//    }


    @GetMapping("/{id}")
    public String getUserInfo(@PathVariable Integer id) {
        ServiceInstance instance = loadBalancerClient.choose("nacos-provider");
        User userInfo = userFeign.getUserInfo(id);
        return "调用" + instance.getServiceId() + "服务，端口号：" + instance.getPort() + "\n" + userInfo.toString();
    }


}
